home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / site / Alias.pm next >
Encoding:
Perl POD Document  |  1999-12-28  |  10.4 KB  |  349 lines

  1.  
  2. package Alias;
  3.  
  4. require 5.003;
  5. require Exporter;
  6. require DynaLoader;
  7.  
  8. @ISA = qw(Exporter DynaLoader);
  9. @EXPORT = qw(alias attr);
  10. @EXPORT_OK = qw(const);
  11.  
  12. $VERSION = $VERSION = '2.3';
  13.  
  14. use Carp;
  15.  
  16. bootstrap Alias;
  17.  
  18. $Alias::KeyFilter = "";
  19. $Alias::AttrPrefix = "";
  20. $Alias::Deref = "";            # don't deref objects
  21.  
  22. sub alias {
  23.   croak "Need even number of args" if @_ % 2;
  24.   my($pkg) = caller;              # for namespace soundness
  25.   while (@_) {
  26.     *{"$pkg\:\:$_[0]"} = (defined($_[1]) and ref($_[1])) ? $_[1] : \$_[1];
  27.     shift; shift;
  28.   }
  29. }
  30.  
  31. sub attr;
  32.  
  33. alias const => \&alias;           # alias the alias :-)
  34.  
  35.  
  36. 1;
  37. __END__
  38.  
  39. =head1 NAME
  40.  
  41. alias - declare symbolic aliases for perl data
  42.  
  43. attr  - auto-declare hash attributes for convenient access
  44.  
  45. const - define compile-time scalar constants
  46.  
  47.  
  48. =head1 SYNOPSIS
  49.  
  50.     use Alias qw(alias const attr);
  51.     alias TEN => $ten, Ten => \$ten, Ten => \&ten,
  52.           Ten => \@ten, Ten => \%ten, TeN => \*ten;
  53.     {
  54.        local @Ten;
  55.        my $ten = [1..10];
  56.        alias Ten => $ten;   # local @Ten
  57.     }
  58.  
  59.     const pi => 3.14, ten => 10;
  60.  
  61.     package Foo;
  62.     use Alias;
  63.     sub new { bless {foo => 1, _bar => [2, 3]}, $_[0] }
  64.     sub a_method {
  65.        my $s = attr shift;
  66.     }
  67.  
  68.     sub b_method {
  69.       local $Alias::KeyFilter = "_";
  70.       local $Alias::AttrPrefix = "main::";
  71.       my $s = attr shift;
  72.     }
  73.  
  74.     sub c_method {
  75.       local $Alias::KeyFilter = sub { $_ = shift; return (/^_/ ? 1 : 0) };
  76.       local $Alias::AttrPrefix = sub {
  77.                                        $_ = shift;
  78.                        s/^_(.+)$/main::$1/;
  79.                        return $_
  80.                      };
  81.       my $s = attr shift;
  82.     }
  83.  
  84.  
  85. =head1 DESCRIPTION
  86.  
  87. Provides general mechanisms for aliasing perl data for convenient access.
  88.  
  89. This module works by putting some values on the symbol table with
  90. user-supplied names.  Values that are references will get dereferenced into
  91. their base types.  This means that a value of C<[1,2,3]> with a name of
  92. "foo" will be made available as C<@foo>, not C<$foo>.
  93.  
  94. The exception to this rule is the default behavior of the C<attr> function,
  95. which will not dereference values which are blessed references (aka
  96. objects).  See L<$Alias::Deref> for how to change this default behavior.
  97.  
  98. =head2 Functions
  99.  
  100. =over 4
  101.  
  102. =item alias
  103.  
  104. Given a list of name => value pairs, declares aliases
  105. in the C<caller>s namespace. If the value supplied is a reference, the
  106. alias is created for the underlying value instead of the reference
  107. itself (there is no need to use this module to alias references--they
  108. are automatically "aliased" on assignment).  This allows the user to
  109. alias most of the basic types.
  110.  
  111. If the value supplied is a scalar compile-time constant, the aliases 
  112. become read-only. Any attempt to write to them will fail with a run time
  113. error. 
  114.  
  115. Aliases can be dynamically scoped by pre-declaring the target variable as
  116. C<local>.  Using C<attr> for this purpose is more convenient, and
  117. recommended.
  118.  
  119. =item attr
  120.  
  121. Given a hash reference, aliases the values of the hash to the names that
  122. correspond to the keys.  It always returns the supplied value.  The aliases
  123. are local to the enclosing block. If any of the values are unblessed
  124. references, they are available as their dereferenced types.  Thus the action
  125. is similar to saying:
  126.  
  127.     alias %{$_[0]}
  128.  
  129. but, in addition, also localizes the aliases, and does not dereference
  130. objects.  Dereferencing of objects can be forced by setting the C<Deref>
  131. option.  See L<$Alias::Deref>.
  132.  
  133. This can be used for convenient access to hash values and hash-based object
  134. attributes.  
  135.  
  136. Note that this makes available the semantics of C<local> subroutines and
  137. methods.  That makes for some nifty possibilities.  We could make truly
  138. private methods by putting anonymous subs within an object.  These subs
  139. would be available within methods where we use C<attr>, and will not
  140. be visible to the outside world as normal methods.  We could forbid 
  141. recursion in methods by always putting an empty sub in the object hash 
  142. with the same key as the method name. This would be useful where a method 
  143. has to run code from other modules, but cannot be certain whether that 
  144. module will call it back again.
  145.  
  146. The default behavior is to create aliases for all the entries in the hash,
  147. in the callers namespace.  This can be controlled by setting a few options.
  148. See L<Configuration Variables> for details.
  149.  
  150. =item const
  151.  
  152. This is simply a function alias for C<alias>, described above.  Provided on
  153. demand at C<use> time, since it reads better for constant declarations.
  154. Note that hashes and arrays cannot be so C<const>rained.
  155.  
  156. =back
  157.  
  158. =head2 Configuration Variables
  159.  
  160. The following configuration variables can be used to control the behavior of
  161. the C<attr> function.  They are typically set after the C<use Alias;>
  162. statement.  Another typical usage is to C<local>ize them in a block so that
  163. their values are only effective within that block.
  164.  
  165. =over 4
  166.  
  167. =item $Alias::KeyFilter
  168.  
  169. Specifies the key prefix used for determining which hash entries will be
  170. interned by C<attr>.  Can be a CODE reference, in which case it will be
  171. called with the key, and the boolean return value will determine if
  172. that hash entry is a candidate attribute.
  173.  
  174. =item $Alias::AttrPrefix
  175.  
  176. Specifies a prefix to prepend to the names of localized attributes created
  177. by C<attr>.  Can be a CODE reference, in which case it will be called with
  178. the key, and the result will determine the full name of the attribute.  The
  179. value can have embedded package delimiters ("::" or "'"), which cause the
  180. attributes to be interned in that namespace instead of the C<caller>s own
  181. namespace. For example, setting it to "main::" makes C<use strict 'vars';>
  182. somewhat more palatable (since we can refer to the attributes as C<$::foo>,
  183. etc., without actually declaring the attributes).
  184.  
  185. =item $Alias::Deref
  186.  
  187. Controls the implicit dereferencing behavior of C<attr>.  If it is set to
  188. "" or 0, C<attr> will not dereference blessed references.  If it is a true
  189. value (anything but "", 0, or a CODE reference), all references will be
  190. made available as their dereferenced types, including values that may be
  191. objects.  The default is "".
  192.  
  193. This option can be used as a filter if it is set to a CODE reference, in
  194. which case it will be called with the key and the value (whenever the value
  195. happens to be a reference), and the boolean return value will determine if
  196. that particular reference must be dereferenced.
  197.  
  198.  
  199. =back
  200.  
  201. =head2 Exports
  202.  
  203. =over 4
  204.  
  205. =item alias
  206.  
  207. =item attr
  208.  
  209. =back
  210.  
  211. =head1 EXAMPLES
  212.  
  213. Run these code snippets and observe the results to become more familiar
  214. with the features of this module.
  215.  
  216.     use Alias qw(alias const attr);
  217.     $ten = 10;
  218.     alias TEN => $ten, Ten => \$ten, Ten => \&ten,
  219.           Ten => \@ten, Ten => \%ten;
  220.     alias TeN => \*ten;  # same as *TeN = *ten
  221.  
  222.     $ten = 20;   
  223.     print "$TEN|$Ten|$ten\n";   # should print "20|20|20"
  224.     sub ten { print "10\n"; }
  225.     @ten = (1..10);
  226.     %ten = (a..j);
  227.     &Ten;                       # should print "10"
  228.     print @Ten, "|", %Ten, "\n";
  229.  
  230.     const _TEN_ => 10;
  231.     eval { $_TEN_ = 20 };
  232.     print $@ if $@;
  233.  
  234.     @DYNAMIC = qw(m n o);
  235.     {
  236.        my $tmp = [ qw(a b c d) ];
  237.        local @DYNAMIC;
  238.        alias DYNAMIC => $tmp, PERM => $tmp;
  239.  
  240.        $DYNAMIC[2] = 'zzz';
  241.        print @$tmp, "|", @DYNAMIC, "|", @PERM, "\n";
  242.  
  243.        @DYNAMIC = qw(p q r);
  244.        print @$tmp, "|", @DYNAMIC, "|", @PERM, "\n";
  245.     }
  246.  
  247.     print @DYNAMIC, "|", @PERM, "\n";
  248.  
  249.     my($lex) = 'abcd';
  250.     $closure = sub { print $lex, "\n" };
  251.     alias NAMEDCLOSURE => \&$closure;
  252.     NAMEDCLOSURE();            # prints "abcd"
  253.     $lex = 'pqrs';
  254.     NAMEDCLOSURE();            # prints "pqrs"
  255.  
  256.     package Foo;
  257.     use Alias;
  258.     sub new { 
  259.       bless 
  260.     { foo => 1, 
  261.           bar => [2,3], 
  262.           buz => { a => 4},
  263.           privmeth => sub { "private" },
  264.           easymeth => sub { die "to recurse or to die, is the question" },
  265.         }, $_[0]; 
  266.     }
  267.  
  268.     sub easymeth {
  269.       my $s = attr shift;    # localizes $foo, @bar, %buz etc with values
  270.       eval { $s->easymeth }; # should fail
  271.       print $@ if $@;
  272.  
  273.       print join '|', $foo, @bar, %buz, $s->privmeth, "\n";
  274.     }
  275.  
  276.     $foo = 6;
  277.     @bar = (7,8);
  278.     %buz = (b => 9);
  279.     Foo->new->easymeth;       # this will not recurse endlessly
  280.  
  281.     print join '|', $foo, @bar, %buz, "\n";
  282.  
  283.     eval { Foo->new->privmeth };
  284.     print $@ if $@;
  285.  
  286.  
  287. =head1 NOTES
  288.  
  289. It is worth repeating that the aliases created by C<alias> and C<const> will
  290. be created in the C<caller>s namespace (we can use the C<AttrPrefix> option to
  291. specify a different namespace for C<attr>).  If that namespace happens to be
  292. C<local>ized, the aliases created will be local to that block.  C<attr>
  293. localizes the aliases for us.
  294.  
  295. Remember that references will be available as their dereferenced types.
  296.  
  297. Aliases cannot be lexical, since, by neccessity, they live on the
  298. symbol table. 
  299.  
  300. Lexicals can be aliased. Note that this provides a means of reversing the
  301. action of anonymous type generators C<\>, C<[]> and C<{}>.  This allows us
  302. to anonymously construct data or code and give it a symbol-table presence
  303. when we choose.
  304.  
  305. Any occurrence of C<::> or C<'> in names will be treated as package
  306. qualifiers, and the value will be interned in that namespace.
  307.  
  308. Remember that aliases are very much like references, only we don't
  309. have to dereference them as often.  Which means we won't have to
  310. pound on the dollars so much.
  311.  
  312. We can dynamically make subroutines and named closures with this scheme.
  313.  
  314. It is possible to alias packages, but that might be construed as
  315. abuse.
  316.  
  317. Using this module will dramatically reduce noise characters in 
  318. object-oriented perl code.
  319.  
  320.  
  321. =head1 BUGS
  322.  
  323. C<use strict 'vars';> is not very usable, since we B<depend> so much
  324. on the symbol table.  You can declare the attributes with C<use vars> to
  325. avoid warnings.  Setting C<$Alias::AttrPrefix> to "main::" is one way
  326. to avoid C<use vars> and frustration.
  327.  
  328. Tied variables cannot be aliased properly, yet.
  329.  
  330.  
  331. =head1 VERSION
  332.  
  333. Version 2.2       1 December 1996
  334.  
  335.  
  336. =head1 AUTHOR
  337.  
  338. Gurusamy Sarathy                gsar@umich.edu
  339.  
  340. Copyright (c) 1995-97 Gurusamy Sarathy. All rights reserved.
  341. This program is free software; you can redistribute it and/or
  342. modify it under the same terms as Perl itself.
  343.  
  344. =head1 SEE ALSO
  345.  
  346. perl(1)
  347.  
  348. =cut
  349.